<?php
namespace Infinity;

abstract class StringHelper {
    public static function startsWith($haystack, $needle) {
        return strpos($haystack, $needle) === 0;
    }
    
    public static function endsWith($haystack, $needle){
        return strrpos($haystack, $needle) === strlen($haystack)-strlen($needle);
    }
    
    public static function contains($haystack, $needle) {
        return strpos($haystack, $needle) !== false;
    }
    
    /**
     * Random String
     *
     * @param int $length
     * @return string
     * */
    public static function random($length = 8) {
        $Result = "";
        $str = "1234567890qwertyuiopasdfghjklzxcvbnm";
        $len = strlen($str);
        for ($i = 0; $i < $length; $i++) {
            $Result .= substr($str, rand(0, $len), 1);
        }
        return $Result;
    }

    public static function maxLength($string, $length=120) {
        $rawStr = strip_tags($string);
        if(drupal_strlen($rawStr) > $length)
            return drupal_substr($rawStr, 0, $length).'...';
        else {
            return $rawStr;
        }
    }

  public static function matchPath($q, $patterns) {
    return drupal_match_path($q, $patterns);
  }
}
